package com.photoeditor.demo.model.store.module;

import android.os.Environment;
import android.text.TextUtils;

import com.common.base.ComponentContext;
import com.common.base.utils.ProcessUtils;
import com.photoeditor.demo.app.TestUser;
import com.photoeditor.demo.model.preference.ChildProcessBroadcastReceiver;
import com.photoeditor.demo.util.FileUtil;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 主页的网络数据请求管理
 * Created by edward on 17-7-13.
 */

public class HomeOnlineManager extends BaseOnlineManager implements ChildProcessBroadcastReceiver.OnMainProcessMsgListener {
    private static final String STORE_ROOT_URL = "superpro-soft/photoeditordemo/homepage-v1";
    private static final String STORE_TEST_ROOT_URL = "superpro-soft/photoeditordemo/homepage-test-v1";
    private static final String FILE_NAME_STORE = "homepage";

    private static final String BANNER_ID = "210";

    private HomeOnlineManager() {
        super();
        ChildProcessBroadcastReceiver.getInstance().registerListener(this);
    }

    private static class SingletonHolder {
        private static final HomeOnlineManager INSTANCE = new HomeOnlineManager();
    }
    public static HomeOnlineManager getInstance() {
        return SingletonHolder.INSTANCE;
    }

    @Override
    protected Config getConfig() {
        return new ConfigBuilder(STORE_ROOT_URL, FILE_NAME_STORE)
                .baseTestRequestUrl(STORE_TEST_ROOT_URL)
                .build();
    }

    public StoreOnlineBean getBannerBean() {
        if (mStoreRootOnlineBean == null) {
            return null;
        }
        List<StoreOnlineBean> childBeans = mStoreRootOnlineBean.getChildModules();
        if (childBeans == null) {
            return null;
        }
        for (StoreOnlineBean storeOnlineBean : childBeans) {
            if (TextUtils.equals(BANNER_ID, storeOnlineBean.getId())) {
                return storeOnlineBean;
            }
        }
        return null;
    }

    public StoreOnlineBean getActionBannerBean() {
        if (mStoreRootOnlineBean == null) {
            return null;
        }
        StoreOnlineBean result = new StoreOnlineBean();
        List<StoreOnlineBean> childBeans = mStoreRootOnlineBean.getChildModules();
        if (childBeans == null) {
            return null;
        }
        for (StoreOnlineBean storeOnlineBean : childBeans) {
            if (TextUtils.equals(STORE_FUNC_KEY_ACTION_BANNER, storeOnlineBean.getName())) {
                result = storeOnlineBean;
            }
        }
        return result;
    }

    public List<StoreOnlineBean> getDataBean() {
        if (mStoreRootOnlineBean == null) {
            return null;
        }
        List<StoreOnlineBean> result = new ArrayList<>();
        List<StoreOnlineBean> childBeans = mStoreRootOnlineBean.getChildModules();
        if (childBeans == null) {
            return null;
        }
        for (StoreOnlineBean storeOnlineBean : childBeans) {
            if (TextUtils.equals(STORE_FUNC_KEY_FILTER, storeOnlineBean.getName())
                    || TextUtils.equals(STORE_FUNC_KEY_STICKER, storeOnlineBean.getName())) {
                result.add(storeOnlineBean);
            }
        }
        return result;
    }

    public List<StoreOnlineBean> getTemplates(int size) {
        if (mStoreRootOnlineBean == null) {
            return null;
        }
        List<StoreOnlineBean> result = new ArrayList<>();
        List<StoreOnlineBean> childBeans = mStoreRootOnlineBean.getChildModules();
        if (childBeans == null) {
            return null;
        }
//        for (StoreOnlineBean storeOnlineBean : childBeans) {
//            if (TextUtils.equals(STORE_FUNC_KEY_TEMPLATE, storeOnlineBean.getName())) {
//                for (StoreOnlineBean childBean : storeOnlineBean.getChildModules()) {
//                    for (StoreOnlineBean targetBean : childBean.getChildModules()) {
//                        if (Integer.valueOf(targetBean.getParam()) >= size) {
//                            result.add(targetBean);
//                        }
//                    }
//                }
//            }
//        }
        return result;
    }

    public String getJumpUrl(StoreOnlineBean storeOnlineBean) {
        if (storeOnlineBean == null) {
            return null;
        }
        if (!TextUtils.isEmpty(storeOnlineBean.getJumpUrl())) {
            return storeOnlineBean.getJumpUrl();
        }
        List<StoreOnlineBean> list = storeOnlineBean.getChildModules();
        if (list == null || list.size() == 0) {
            return null;
        }
        return getJumpUrl(list.get(0));
    }

    @Override
    protected synchronized void notifyDataListener(boolean needUpdate) {
        super.notifyDataListener(needUpdate);
        if (needUpdate && ProcessUtils.isMainProcess(ComponentContext.getContext())) {
            ChildProcessBroadcastReceiver.getInstance().sendBroadcastMsg(ChildProcessBroadcastReceiver.TYPE_REFRESH_HOMEPAGE_DATA);
        }
    }

    @Override
    public void onMainProcessMsgReceive(int type, int status, Serializable data) {
        if (type == ChildProcessBroadcastReceiver.TYPE_REFRESH_HOMEPAGE_DATA) {
            loadData();
            notifyDataListener(true);
        }
    }

    @Override
    protected boolean parse(String text) {
        if (TestUser.isTestUser()) {
            // 如果是测试买量用户，尝试读取本地存储卡根目录下json文件，用于测试
            String testFile = Environment.getExternalStorageDirectory() + File.separator + FILE_NAME_STORE;
            String testData = FileUtil.readFileToString(testFile);
            if (!TextUtils.isEmpty(testData)) {
                return super.parse(testData);
            } else {
                return super.parse(text);
            }
        } else {
            return super.parse(text);
        }
    }
}
